﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Infrastructure.Library;
using UtilLib;

namespace Finance.Core
{
    public partial class FinanceItem : BusinessBase<FinanceItem, String>, IComparable<FinanceItem>
    {
        private static readonly object lockObj = new object();

        #region Properties
        private DateTime date;
        public DateTime Date
        {
            get { return date; }
            set { base.SetValue("Date", value, ref date); }
        }
        private FinanceType type;
        public FinanceType Type
        {
            get { return type; }
            set { base.SetValue("Type", value, ref type); }
        }

        private Decimal amount;
        public Decimal Amount
        {
            get { return amount; }
            set { base.SetValue("Amount", value, ref amount); }
        }

        private String desc;
        public String Desc
        {
            get { return desc; }
            set { base.SetValue("Desc", value, ref desc); }
        }

        private static List<FinanceItem> list;
        public static List<FinanceItem> List
        {
            get
            {
                if (list == null)
                {
                    lock (lockObj)
                    {
                        if (list == null)
                        {
                            list = FinanceService.GetAllFinance();
                        }
                    }
                }

                return list;
            }
        }
        #endregion

        #region Methods
        #region BusinessBase Abstract
        protected override void DataDelete()
        {
            if (this.Deleted)
            {
                FinanceService.Delete(this);
            }
            this.Dispose();
        }

        protected override void DataInsert()
        {
            if (this.New)
            {
                FinanceService.Insert(this);
            }
        }

        protected override FinanceItem DataSelect(string id)
        {
            return FinanceService.GetFinanceItem(id);
        }

        protected override void DataUpdate()
        {
            if (this.IsChanged)
            {
                FinanceService.Update(this);
            }
        }

        protected override void ValidationRules()
        {
            this.AddRule("Type", "Name must be set", StringUtil.IsNullOrWhitespace(this.Type.ToString()));
            this.AddRule("Date", "Date must be set", this.date == System.DateTime.MinValue);
        }
        #endregion

        public static void SaveAll()
        {
            FinanceService.SaveAllFinance();
        }
        #region IComparable<FinanceItem> Members

        public int CompareTo(FinanceItem other)
        {
            return this.Date.CompareTo(other.Date);
        }

        #endregion

        #endregion
    }
}
